package yl.hs.bmipfull.service.laboratory.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratory.IProductCateDao;
import yl.hs.bmipfull.daos.laboratory.IProductConfigDao;
import yl.hs.bmipfull.daos.laboratory.IProductConfigDetailsDao;
import yl.hs.bmipfull.daos.laboratory.ITaskConfigDetailsDao;
import yl.hs.bmipfull.daos.material.IRawMaterialCateDao;
import yl.hs.bmipfull.daos.production.IProductionTaskFertilizerDao;
import yl.hs.bmipfull.pojo.laboratory.ProductConfigDetailsEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialCateEntity;
import yl.hs.bmipfull.pojo.salemanage.ContractControlInfoEntity;
import yl.hs.bmipfull.service.laboratory.IProductConfigDetailsService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  产品配比明细表服务层接口实现<br/>
 * 创建时间: 2021年05月26日 10时36分49秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ProductConfigDetailsServiceImpl extends LaboratoryBaseService implements IProductConfigDetailsService {

    @Autowired
    IProductConfigDetailsDao dao;

    @Autowired
    IProductConfigDao configDao;
    @Autowired
    IRawMaterialCateDao rawMaterialcatedao;
    @Autowired
    IRawMaterialCateDao rmCateDao;
    @Autowired
    IProductionTaskFertilizerDao taskFertilizerDao;
    @Autowired
    ITaskConfigDetailsDao detailsDao;

    @Override
    public List<Object> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductConfigDetailsEntity.class);
        var result =  dao.queryList(pageQuery.getQuery());
        List<Object> data = new ArrayList<>();
        result.forEach(t->{
            var entity = t.toMap();
            var cate =  rawMaterialcatedao.findBySN(t.getRmCate());
            if(cate!=null) {
                entity.put("RmCateName", cate.getCateName());
            }else{
                entity.put("RmCateName", "");
            }
            data.add(entity);
        });
        return data;
    }


    @Override
    public ProductConfigDetailsEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public Map<String, Object> findByConfigSn(String configSn,String tasksn) {
        var productionConfig = configDao.findBySN(configSn);
        Map<String, Object> configMap = new HashMap<>();
        var cateList = rmCateDao.querySelect();
        if (productionConfig != null) {
            var proMap = productionConfig.toMap();
            var detailList = dao.queryListByConfigSn(configSn);
            proMap.put("details", fillRmCateName(cateList, detailList));
            var task = taskFertilizerDao.findBySN(tasksn);
            var sc =  detailsDao.queryByTaskConfigSn(task.getTaskPbSn());
            proMap.put("scdetails", sc);
            configMap.put("production", proMap);
        }

        return configMap;
    }

    private List<Map<String, Object>> fillRmCateName(List<RawMaterialCateEntity> cateList, List<ProductConfigDetailsEntity> detailList) {

        List<Map<String, Object>> details = new ArrayList<>();
        for (var item : detailList) {
            var map = item.toMap();
            var cate = cateList.stream().filter(t -> t.getSn().equals(item.getRmCate())).findFirst();
            String cateName = "";
            if (cate.isPresent()) {
                cateName = cate.get().getCateName();
            }
            map.put("rmCateName", cateName);
            details.add(map);
        }
        return details;
    }

    @Override
    public Integer saveProductConfigDetails(List<ProductConfigDetailsEntity> entities, String ConfigSn) throws Exception {
        Integer result = 0;
        //删除产品配比明细信息
        if (dao.deleteByConfigSN(ConfigSn) > 0) {
            writeLog(ActionType.DELETE, ConfigSn, "删除产品配比明细信息", ConfigSn, ContractControlInfoEntity.class);
        }
        for (ProductConfigDetailsEntity entity : entities) {
            entity.setSn(queryNewSN(ProductConfigDetailsEntity.class));
            result += dao.insertNew(entity);
            writeLog(ActionType.CREATE, ConfigSn, "插入产品配比明细信息", Tl.toJson(entity), ContractControlInfoEntity.class);
        }
        return result;
    }

    @Override
    public Integer deleteByConfigSn(String configSn) {
        return dao.deleteByConfigSN(configSn);
    }

    @Override
    public List<ProductConfigDetailsEntity> queryByConfigSn(String configSn) {
        return dao.queryListByConfigSn(configSn);
    }
}